package com.example.consumermovie.controller;

import com.example.consumermovie.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("provider-user");
        System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        return this.restTemplate.getForObject("http://provider-user/simple/"+id,User.class);
    }
    @GetMapping("/test")
    public String test(){
        ServiceInstance instance1 = this.loadBalancerClient.choose("provider-user");
        System.out.println("111:"+instance1.getHost()+":"+instance1.getPort()+":"+instance1.getServiceId());

        ServiceInstance instance2 = this.loadBalancerClient.choose("provider-user2");
        System.out.println("222:"+instance2.getHost()+":"+instance2.getPort()+":"+instance2.getServiceId());

        return "1";
    }
    @GetMapping("/sidecar")
    public String getSidecar(){
        return this.restTemplate.getForObject("http://sidecar",String.class);
    }
}
